package com.ssky.bean.validator.core.checker;

import com.ssky.bean.validator.core.IMethodCheckerHandler;
import com.ssky.bean.validator.core.IValidatorContext;
import com.ssky.bean.validator.core.exception.VldException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author: ssky
 * @Date: 2021/07/26
 * @Description:
 */
public class MethodChecker extends AbstractChecker {
    private static final Logger L = LoggerFactory.getLogger(MethodChecker.class);
    private IMethodCheckerHandler methodCheckerHandler;

    @Override
    void initz() throws Exception {

    }

    /**
     * 具体规则执行器
     *
     * @param v   规则校验的当前值
     * @param l   当前值作为属性的所在对象
     * @param b   当前值所在的顶级javaBean对象
     * @param ctx 上下文（针对具体规则的实现要求，可基于接口IValidatorContext扩展）
     * @return 校验通过返回true, 不通过返回false
     */
    @Override
    public boolean process(Object v, Object l, Object b, IValidatorContext ctx) {

        boolean rst;
        try {
            rst = methodCheckerHandler.check(b,ctx);
        } catch (Exception e) {
            L.error("MethodChecker ruleId="+rule.getId()+" ocurred exception -->",e);
            throw new VldException(e.getMessage());
        }
        return rst;
    }


    public void setMethodCheckerHandler(IMethodCheckerHandler methodCheckerHandler) {
        this.methodCheckerHandler = methodCheckerHandler;
    }
}
